using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CUSTOMS.Modules.QLHS.Components.QLCT
{
    public partial class ChungTu
    {
        #region Private members.

        protected int _SoToKhai;
        protected string _MaHaiQuan = string.Empty;
        protected string _MaLoaiHinh = string.Empty;
        protected int _NamDangKy;
        protected Guid _PhienBan = Guid.Empty;
        protected int _SoTo;
        protected string _NoiGiao = string.Empty;
        protected string _NguoiGiao = string.Empty;
        protected string _NoiNhan = string.Empty;
        protected string _NguoiNhan = string.Empty;
        protected DateTime _NgayBanGiao = new DateTime(1753, 1, 1);
        protected string _TrangThai = string.Empty;
        protected string _ViTriLuuTru = string.Empty;
        protected string _GhiChu = string.Empty;
        protected string _NguoiNhapDuLieu = string.Empty;
        protected DateTime _NgayNhapDuLieu = new DateTime(1753, 1, 1);

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int SoToKhai
        {
            set { this._SoToKhai = value; }
            get { return this._SoToKhai; }
        }

        public string MaHaiQuan
        {
            set { this._MaHaiQuan = value; }
            get { return this._MaHaiQuan; }
        }

        public string MaLoaiHinh
        {
            set { this._MaLoaiHinh = value; }
            get { return this._MaLoaiHinh; }
        }

        public int NamDangKy
        {
            set { this._NamDangKy = value; }
            get { return this._NamDangKy; }
        }

        public Guid PhienBan
        {
            set { this._PhienBan = value; }
            get { return this._PhienBan; }
        }

        public int SoTo
        {
            set { this._SoTo = value; }
            get { return this._SoTo; }
        }

        public string NoiGiao
        {
            set { this._NoiGiao = value; }
            get { return this._NoiGiao; }
        }

        public string NguoiGiao
        {
            set { this._NguoiGiao = value; }
            get { return this._NguoiGiao; }
        }

        public string NoiNhan
        {
            set { this._NoiNhan = value; }
            get { return this._NoiNhan; }
        }

        public string NguoiNhan
        {
            set { this._NguoiNhan = value; }
            get { return this._NguoiNhan; }
        }

        public DateTime NgayBanGiao
        {
            set { this._NgayBanGiao = value; }
            get { return this._NgayBanGiao; }
        }

        public string TrangThai
        {
            set { this._TrangThai = value; }
            get { return this._TrangThai; }
        }

        public string ViTriLuuTru
        {
            set { this._ViTriLuuTru = value; }
            get { return this._ViTriLuuTru; }
        }

        public string GhiChu
        {
            set { this._GhiChu = value; }
            get { return this._GhiChu; }
        }

        public string NguoiNhapDuLieu
        {
            set { this._NguoiNhapDuLieu = value; }
            get { return this._NguoiNhapDuLieu; }
        }

        public DateTime NgayNhapDuLieu
        {
            set { this._NgayNhapDuLieu = value; }
            get { return this._NgayNhapDuLieu; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static ChungTu Load(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, Guid phienBan)
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, soToKhai);
            db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, maHaiQuan);
            db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, maLoaiHinh);
            db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, namDangKy);
            db.AddInParameter(dbCommand, "@PhienBan", SqlDbType.UniqueIdentifier, phienBan);
            ChungTu entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new ChungTu();
                if (!reader.IsDBNull(reader.GetOrdinal("SoToKhai"))) entity.SoToKhai = reader.GetInt32(reader.GetOrdinal("SoToKhai"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) entity.MaHaiQuan = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaLoaiHinh"))) entity.MaLoaiHinh = reader.GetString(reader.GetOrdinal("MaLoaiHinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("NamDangKy"))) entity.NamDangKy = reader.GetInt32(reader.GetOrdinal("NamDangKy"));
                if (!reader.IsDBNull(reader.GetOrdinal("PhienBan"))) entity.PhienBan = reader.GetGuid(reader.GetOrdinal("PhienBan"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTo"))) entity.SoTo = reader.GetInt32(reader.GetOrdinal("SoTo"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiGiao"))) entity.NoiGiao = reader.GetString(reader.GetOrdinal("NoiGiao"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguoiGiao"))) entity.NguoiGiao = reader.GetString(reader.GetOrdinal("NguoiGiao"));
                if (!reader.IsDBNull(reader.GetOrdinal("NoiNhan"))) entity.NoiNhan = reader.GetString(reader.GetOrdinal("NoiNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguoiNhan"))) entity.NguoiNhan = reader.GetString(reader.GetOrdinal("NguoiNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayBanGiao"))) entity.NgayBanGiao = reader.GetDateTime(reader.GetOrdinal("NgayBanGiao"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThai"))) entity.TrangThai = reader.GetString(reader.GetOrdinal("TrangThai"));
                if (!reader.IsDBNull(reader.GetOrdinal("ViTriLuuTru"))) entity.ViTriLuuTru = reader.GetString(reader.GetOrdinal("ViTriLuuTru"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguoiNhapDuLieu"))) entity.NguoiNhapDuLieu = reader.GetString(reader.GetOrdinal("NguoiNhapDuLieu"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayNhapDuLieu"))) entity.NgayNhapDuLieu = reader.GetDateTime(reader.GetOrdinal("NgayNhapDuLieu"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------


        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertChungTu(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, Guid phienBan, int soTo, string noiGiao, string nguoiGiao, string noiNhan, string nguoiNhan, DateTime ngayBanGiao, string trangThai, string viTriLuuTru, string ghiChu, string nguoiNhapDuLieu, DateTime ngayNhapDuLieu)
        {
            ChungTu entity = new ChungTu();
            entity.SoToKhai = soToKhai;
            entity.MaHaiQuan = maHaiQuan;
            entity.MaLoaiHinh = maLoaiHinh;
            entity.NamDangKy = namDangKy;
            entity.PhienBan = phienBan;
            entity.SoTo = soTo;
            entity.NoiGiao = noiGiao;
            entity.NguoiGiao = nguoiGiao;
            entity.NoiNhan = noiNhan;
            entity.NguoiNhan = nguoiNhan;
            entity.NgayBanGiao = ngayBanGiao;
            entity.TrangThai = trangThai;
            entity.ViTriLuuTru = viTriLuuTru;
            entity.GhiChu = ghiChu;
            entity.NguoiNhapDuLieu = nguoiNhapDuLieu;
            entity.NgayNhapDuLieu = ngayNhapDuLieu;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
            db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
            db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
            db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
            db.AddInParameter(dbCommand, "@PhienBan", SqlDbType.UniqueIdentifier, PhienBan);
            db.AddInParameter(dbCommand, "@SoTo", SqlDbType.Int, SoTo);
            db.AddInParameter(dbCommand, "@NoiGiao", SqlDbType.VarChar, NoiGiao);
            db.AddInParameter(dbCommand, "@NguoiGiao", SqlDbType.NVarChar, NguoiGiao);
            db.AddInParameter(dbCommand, "@NoiNhan", SqlDbType.VarChar, NoiNhan);
            db.AddInParameter(dbCommand, "@NguoiNhan", SqlDbType.NVarChar, NguoiNhan);
            db.AddInParameter(dbCommand, "@NgayBanGiao", SqlDbType.DateTime, NgayBanGiao.Year <= 1753 ? DBNull.Value : (object)NgayBanGiao);
            db.AddInParameter(dbCommand, "@TrangThai", SqlDbType.VarChar, TrangThai);
            db.AddInParameter(dbCommand, "@ViTriLuuTru", SqlDbType.VarChar, ViTriLuuTru);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
            db.AddInParameter(dbCommand, "@NguoiNhapDuLieu", SqlDbType.VarChar, NguoiNhapDuLieu);
            db.AddInParameter(dbCommand, "@NgayNhapDuLieu", SqlDbType.DateTime, NgayNhapDuLieu.Year <= 1753 ? DBNull.Value : (object)NgayNhapDuLieu);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateChungTu(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, Guid phienBan, int soTo, string noiGiao, string nguoiGiao, string noiNhan, string nguoiNhan, DateTime ngayBanGiao, string trangThai, string viTriLuuTru, string ghiChu, string nguoiNhapDuLieu, DateTime ngayNhapDuLieu)
        {
            ChungTu entity = new ChungTu();
            entity.SoToKhai = soToKhai;
            entity.MaHaiQuan = maHaiQuan;
            entity.MaLoaiHinh = maLoaiHinh;
            entity.NamDangKy = namDangKy;
            entity.PhienBan = phienBan;
            entity.SoTo = soTo;
            entity.NoiGiao = noiGiao;
            entity.NguoiGiao = nguoiGiao;
            entity.NoiNhan = noiNhan;
            entity.NguoiNhan = nguoiNhan;
            entity.NgayBanGiao = ngayBanGiao;
            entity.TrangThai = trangThai;
            entity.ViTriLuuTru = viTriLuuTru;
            entity.GhiChu = ghiChu;
            entity.NguoiNhapDuLieu = nguoiNhapDuLieu;
            entity.NgayNhapDuLieu = ngayNhapDuLieu;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_HAIQUAN_QLHS_ChungTu_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
            db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
            db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
            db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
            db.AddInParameter(dbCommand, "@PhienBan", SqlDbType.UniqueIdentifier, PhienBan);
            db.AddInParameter(dbCommand, "@SoTo", SqlDbType.Int, SoTo);
            db.AddInParameter(dbCommand, "@NoiGiao", SqlDbType.VarChar, NoiGiao);
            db.AddInParameter(dbCommand, "@NguoiGiao", SqlDbType.NVarChar, NguoiGiao);
            db.AddInParameter(dbCommand, "@NoiNhan", SqlDbType.VarChar, NoiNhan);
            db.AddInParameter(dbCommand, "@NguoiNhan", SqlDbType.NVarChar, NguoiNhan);
            db.AddInParameter(dbCommand, "@NgayBanGiao", SqlDbType.DateTime, NgayBanGiao.Year == 1753 ? DBNull.Value : (object)NgayBanGiao);
            db.AddInParameter(dbCommand, "@TrangThai", SqlDbType.VarChar, TrangThai);
            db.AddInParameter(dbCommand, "@ViTriLuuTru", SqlDbType.VarChar, ViTriLuuTru);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
            db.AddInParameter(dbCommand, "@NguoiNhapDuLieu", SqlDbType.VarChar, NguoiNhapDuLieu);
            db.AddInParameter(dbCommand, "@NgayNhapDuLieu", SqlDbType.DateTime, NgayNhapDuLieu.Year == 1753 ? DBNull.Value : (object)NgayNhapDuLieu);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateChungTu(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, Guid phienBan, int soTo, string noiGiao, string nguoiGiao, string noiNhan, string nguoiNhan, DateTime ngayBanGiao, string trangThai, string viTriLuuTru, string ghiChu, string nguoiNhapDuLieu, DateTime ngayNhapDuLieu)
        {
            ChungTu entity = new ChungTu();
            entity.SoToKhai = soToKhai;
            entity.MaHaiQuan = maHaiQuan;
            entity.MaLoaiHinh = maLoaiHinh;
            entity.NamDangKy = namDangKy;
            entity.PhienBan = phienBan;
            entity.SoTo = soTo;
            entity.NoiGiao = noiGiao;
            entity.NguoiGiao = nguoiGiao;
            entity.NoiNhan = noiNhan;
            entity.NguoiNhan = nguoiNhan;
            entity.NgayBanGiao = ngayBanGiao;
            entity.TrangThai = trangThai;
            entity.ViTriLuuTru = viTriLuuTru;
            entity.GhiChu = ghiChu;
            entity.NguoiNhapDuLieu = nguoiNhapDuLieu;
            entity.NgayNhapDuLieu = ngayNhapDuLieu;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
            db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
            db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
            db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
            db.AddInParameter(dbCommand, "@PhienBan", SqlDbType.UniqueIdentifier, PhienBan);
            db.AddInParameter(dbCommand, "@SoTo", SqlDbType.Int, SoTo);
            db.AddInParameter(dbCommand, "@NoiGiao", SqlDbType.VarChar, NoiGiao);
            db.AddInParameter(dbCommand, "@NguoiGiao", SqlDbType.NVarChar, NguoiGiao);
            db.AddInParameter(dbCommand, "@NoiNhan", SqlDbType.VarChar, NoiNhan);
            db.AddInParameter(dbCommand, "@NguoiNhan", SqlDbType.NVarChar, NguoiNhan);
            db.AddInParameter(dbCommand, "@NgayBanGiao", SqlDbType.DateTime, NgayBanGiao.Year == 1753 ? DBNull.Value : (object)NgayBanGiao);
            db.AddInParameter(dbCommand, "@TrangThai", SqlDbType.VarChar, TrangThai);
            db.AddInParameter(dbCommand, "@ViTriLuuTru", SqlDbType.VarChar, ViTriLuuTru);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
            db.AddInParameter(dbCommand, "@NguoiNhapDuLieu", SqlDbType.VarChar, NguoiNhapDuLieu);
            db.AddInParameter(dbCommand, "@NgayNhapDuLieu", SqlDbType.DateTime, NgayNhapDuLieu.Year == 1753 ? DBNull.Value : (object)NgayNhapDuLieu);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteChungTu(int soToKhai, string maHaiQuan, string maLoaiHinh, int namDangKy, Guid phienBan)
        {
            ChungTu entity = new ChungTu();
            entity.SoToKhai = soToKhai;
            entity.MaHaiQuan = maHaiQuan;
            entity.MaLoaiHinh = maLoaiHinh;
            entity.NamDangKy = namDangKy;
            entity.PhienBan = phienBan;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_HAIQUAN_QLHS_ChungTu_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SoToKhai", SqlDbType.Int, SoToKhai);
            db.AddInParameter(dbCommand, "@MaHaiQuan", SqlDbType.Char, MaHaiQuan);
            db.AddInParameter(dbCommand, "@MaLoaiHinh", SqlDbType.Char, MaLoaiHinh);
            db.AddInParameter(dbCommand, "@NamDangKy", SqlDbType.Int, NamDangKy);
            db.AddInParameter(dbCommand, "@PhienBan", SqlDbType.UniqueIdentifier, PhienBan);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion
    }
}